export const ab2hex = (buffer) => {
	const hexArr = Array.prototype.map.call(
		new Uint8Array(buffer),
		function(bit) {
			return ('00' + bit.toString(16)).slice(-2)
		}
	)
	return hexArr.join('')
}
//arraybuffer 转字符串
export const hexCharCodeToStr = (hexCharCodeStr) => {
	let trimedStr = hexCharCodeStr.trim();
	let rawStr =
		trimedStr.substr(0, 2).toLowerCase() === "0x" ?
		trimedStr.substr(2) :
		trimedStr;
	let len = rawStr.length;
	if (len % 2 !== 0) {
		return "";
	}
	let curCharCode;
	let resultStr = [];
	for (let i = 0; i < len; i = i + 2) {
		curCharCode = parseInt(rawStr.substr(i, 2), 16); // ASCII Code Value
		let str5 = String.fromCharCode(curCharCode)
		if (str5.startsWith('\n') == false) {
			resultStr.push(String.fromCharCode(curCharCode));
		}
	}
	return resultStr.join("");
}
export const ab2str = (buf) => {
	let arrayBuffer = new ArrayBuffer(buf);
	return Array.prototype.slice.call(new Uint8Array(arrayBuffer ));
	
}

//字符串转arraybuffer

export const stringTobuffer = (str) => {
	// 首先将字符串转为16进制
	let val = ""
	for (let i = 0; i < str.length; i++) {
		if (val === '') {
			val = str.charCodeAt(i).toString(16)
		} else {
			val += ',' + str.charCodeAt(i).toString(16)
		}
	}
	// console.log(val)
	// 将16进制转化为ArrayBuffer
	return new Uint8Array(val.match(/[\da-f]{2}/gi).map(function(h) {
		return parseInt(h, 16)
	})).buffer;

	/* var bytes = new Array();
	var len, c;
	len = str.length;
	for (var i = 0; i < len; i++) {
		c = str.charCodeAt(i);
		if (c >= 0x010000 && c <= 0x10FFFF) {
			bytes.push(((c >> 18) & 0x07) | 0xF0);
			bytes.push(((c >> 12) & 0x3F) | 0x80);
			bytes.push(((c >> 6) & 0x3F) | 0x80);
			bytes.push((c & 0x3F) | 0x80);
		} else if (c >= 0x000800 && c <= 0x00FFFF) {
			bytes.push(((c >> 12) & 0x0F) | 0xE0);
			bytes.push(((c >> 6) & 0x3F) | 0x80);
			bytes.push((c & 0x3F) | 0x80);
		} else if (c >= 0x000080 && c <= 0x0007FF) {
			bytes.push(((c >> 6) & 0x1F) | 0xC0);
			bytes.push((c & 0x3F) | 0x80);
		} else {
			bytes.push(c & 0xFF);
		}
	}
	let arrayBuffer = new Uint8Array(bytes).buffer;
	return arrayBuffer; */
}

//10进制转16进制
export const decimalToHex = (decimal) => {
	return decimal.toString(16);
}

//数组16进制转buffer
// export const arrTobuffer(arr) {
// 	const buffer = new ArrayBuffer(arr.length)
// 	const dataView = new DataView(buffer)

// 	for (var i = 0; i < arr.length; i++) {
// 		dataView.setUint8(i, arr[i])
// 	}
// 	return buffer
// }

//年月日时分秒，日期转16进制
export const dateToHex = (date) => {
	// const year = date.getFullYear().toString(16);
	// const year1='0x0'+year.substring(0,1)
	// const year2='0x'+year.substring(1,3)
	// const month = '0x' + (date.getMonth() + 1).toString(16).padStart(2, '0'); // 月份是从0开始的
	// const day = '0x' + date.getDate().toString(16).padStart(2, '0');
	const hours = '0x' + date.getHours().toString(16).padStart(2, '0');
	const minutes = '0x' + date.getMinutes().toString(16).padStart(2, '0');
	const seconds = '0x' + date.getSeconds().toString(16).padStart(2, '0');
	// return [year1,year2, month, day, hours, minutes, seconds];
	return [hours, minutes, seconds];
}
//时分，转16进制
export const timeToHex = (time) => {
	const date = time.split(":")
	const hours = '0x' + Number(date[0]).toString(16).padStart(2, '0');
	const minutes = '0x' + Number(date[1]).toString(16).padStart(2, '0');
	return [hours, minutes];
}



//CheckSum的計算
export const calcCheckSum = (arr) => {
	let sum = 0;
	arr.forEach((item, index) => {
		sum += parseInt(item, 16);
	});
	let result = sum & 0xff;
	//转换为Hex 16进制（想要10进制可以把toString去掉）
	return result.toString(16);
}

//字符串转16进制
export const stringToHex = (str) => {
	let val = ""
	console.log(typeof(str),str,'str')
	for (let i = 0; i < str.length; i++) {
		if (val === '') {
			val = str.charCodeAt(i).toString(16)
		} else {
			val += ',' + str.charCodeAt(i).toString(16)
		}
	}
	return val.split(",").map(item => '0x' + item)
}